package com.financing.controller.app;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.financing.common.CONST;
import com.financing.controller.invest.BidController;
import com.financing.model.Data;
import com.financing.model.invest.Bid;
import com.financing.model.system.Notice;
import com.financing.service.invest.BidService;
import com.financing.service.invest.RepayService;
import com.financing.service.system.NoticeService;
import com.financing.service.system.SliderService;
import com.financing.service.user.UserService;
import com.financing.util.DateUtil;
import com.financing.util.Json;
import com.financing.util.Str;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;

public class HomeController extends Controller {
	private static BidService bid = new BidService();
	private static UserService user = new UserService();
	private static SliderService slider = new SliderService();
	private static RepayService repay = new RepayService();
	private static NoticeService notice = new NoticeService();

	/**
	 * PC页面跳转控制器
	 * @throws Exception
	 */
	@Clear
	@Before({SessionInViewInterceptor.class})
	public void index() throws Exception {
		String page = getPara(0,"index/index"); 
		String page2 = getPara(1); 
		setAttr("para",getParaMap());
		
		if(Str.nb(page2)){
			render(page+"/"+page2+".html");
		}else{
			render(page+".html");
		}
	}
	

	@Clear
	public void home() throws Exception {
		
		
		// 获取新手标
		List<Bid> first = bid.list(1, 1, Data.putStr("type","first","status","waiting*trading*full*repaying")," order by period desc").getList();
		
		// 获取短期标
		List<Bid> bids = bid.list(1, 4, Data.putStr("type","public","status","waiting*full*trading*repaying")," order by field(`status`,'waiting','trading','full','repaying','finish'),type,start_time desc").getList();
	    
		Map<String,String> params = Data.putStr("status","enable");

		// 行业报道
		params.put("type", "pc_news");
		Page<Notice> news = notice.list(1, 5, params);

		// 平台公告
		params.put("type", "pc_notice"); 
		Page<Notice> notices = notice.list(1, 5, params);
		
		// 投资百科
		params.put("type", "pc_encyclopedias"); 
		Page<Notice> encyclopedias = notice.list(1, 5, params);
		
		// 公司动态
		params.put("type", "pc_report"); 
		Page<Notice> reports = notice.list(1, 5, params);
		
		// 精彩活动
		params.put("type", "pc_activity"); 
		Page<Notice> activitys = notice.list(1, 5, params);
		
		// 项目上线
		params.put("type", "pc_project"); 
		Page<Notice> projects = notice.list(1, 5, params);

		// 第一个项目（带图）
		Notice picProject = Notice.me.findFirst("select * from notice where pc_pic<>'' and type='pc_project' order by create_time desc");
		
		Map<String,Object> res = new HashMap<String,Object>();
		
		// 累计注册用户
		res.put("total_users", user.total()+Integer.valueOf(CONST.find("REG_NUM")));
		
		// 累计投资
		res.put("total_bid_amount", bid.total());
		
		// 赚取收益
		res.put("total_repay_amount", repay.total()+Double.valueOf(CONST.find("REPAY_AMOUNT")));
		
		renderJson(Json.ok("数据获取成功！", Data.put("now",DateUtil.getNowDateAndTime(), "sliders",slider.findEnable("web"), "statistics",res,"first",first,"bids",bids,"picProject",picProject,"projects",projects,"news",news,"notices",notices,"encyclopedias",encyclopedias,"reports",reports,"activitys",activitys)));
	}

	/**
	 * 投资项目列表
	 */
	@Clear
	public void bids() { 
		
		// 刷新标的状态
		bid.refresh();
		
		// 获取参数
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		int dayLimit = getParaToInt("day",0);
		int rateStart = getParaToInt("rateStart",0);
		int rateEnd = getParaToInt("rateEnd",0);
		String type = getPara("type","");
		SqlPara sqlPara = new SqlPara();
		String sql ="select *,apply_unit+virtual_unit as avunit from bid where (status='trading' or status='repaying' or status='waiting' or status='finish') ";
		if(dayLimit>0) {
			sql +=" and day_limit=? ";
			sqlPara.addPara(dayLimit);
		} 
		if(rateStart>0 && rateEnd>0) {
			sql +=" and rate+plus_rate>=? and rate+plus_rate<? ";
			sqlPara.addPara(rateStart).addPara(rateEnd);
		} 
		if(Str.nb(type)) {
			sql +=" and type=?";
			sqlPara.addPara(type);
		} 
		sqlPara.setSql(sql+" order by status desc, start_time desc");
		Page<Bid> p = Bid.me.paginate(page, size, sqlPara);
		
	    // 仅第一页内容需要排序
		if(page==1){
			BidController.sortBid(p.getList(),0);
		}
		
		// 设置时间戳
		for(Bid b : p.getList()) {
			if(b.getStatus().equals("waiting")) {
				b.put("start_time_stamp", Math.abs(b.getStartTime().getTime()-Long.valueOf(DateUtil.getTimeStamp())));
			}
		}
		
		renderJson(Json.ok("数据获取成功！", Data.put("page",p,"now",DateUtil.getNowDateAndTime())));
	}
}
 